package com.lsa.shenn.common.perm;

public class SetEnumerator
{
	private final int bound;
	private final int sizeOfSet;
	
	private int currentSeq;

	public SetEnumerator(int sizeOfSet)
	{
		this.sizeOfSet = sizeOfSet;
		this.bound = 2 << (sizeOfSet-1) ;
		this.currentSeq = -1;
	}
	
	public int[] nextSet()
	{
		++this.currentSeq;
		
		if (this.currentSeq == this.bound)
		{
			return null;
		}
		
		int[] set = new int[this.sizeOfSet];
		
		int mask = 1;
		int pos = this.sizeOfSet;
		
		while(this.currentSeq >= mask)
		{
			--pos;
			
			if ((this.currentSeq & mask) != 0)
			{
				set[pos] = 1;
			}
			
			mask = mask << 1;
		}
		
		return set;
	}
}
